import { defineStore } from 'pinia'
import { routerStore } from './router'
import { ITab } from './type/index'

export type TabsState = {
    tabsList: Array<ITab>
}

export const tabStore = defineStore('tabStore', {
    state: (): TabsState => {
        return {
            tabsList: []
        }
    },
    actions: {
        addTab(tab: ITab) {
            // 判断是否已经存在，如果不存在，才放入
            if (this.tabsList.some((item) => item.path === tab.path)) return

            this.tabsList.push(tab)
        },
        delTab(path: string, name: string) {
            const rStore = routerStore()
            // 重新设置选项卡数据
            this.tabsList = this.tabsList.filter(
                (tab: ITab) => tab.path !== path
            )
            rStore.delKeepAliveRouters(name)
        },
        currentRefresh(rightActive: string) {
            const rStore = routerStore()
            this.tabsList.forEach((item) => {
                if (item.path == rightActive) {
                    rStore.delKeepAliveRouters(item.name)
                }
            })
            return rightActive
        },
        closeAll() {
            const rStore = routerStore()
            this.tabsList.forEach((item) => {
                rStore.delKeepAliveRouters(item.name)
            })
            this.tabsList.splice(1, this.tabsList.length - 1)
        },
        closeLeft(rightActive: string, activeTab: string) {
            const rStore = routerStore()
            let rightIndex = this.tabsList.findIndex((item) => {
                if (item.path != rightActive && item.path !== activeTab) {
                    // console.log('item=', item)
                    rStore.delKeepAliveRouters(item.name)
                }
                return item.path === rightActive
            })
            let activeIndex = this.tabsList.findIndex((item) => {
                return item.path === activeTab
            })
            // const len = this.tabsList.length
            rightIndex = rightIndex
            activeIndex = activeIndex
            // console.log('rightIndex=', rightIndex)
            // console.log('activeIndex=', activeIndex)
            if (rightIndex > 1) {
                if (activeIndex < rightIndex) {
                    if (activeIndex > 1) {
                        this.tabsList.splice(
                            activeIndex + 1,
                            rightIndex - activeIndex - 1
                        )
                        this.tabsList.splice(1, activeIndex - 1)
                    } else {
                        this.tabsList.splice(activeIndex + 1, rightIndex - 2)
                    }
                } else {
                    this.tabsList.splice(1, rightIndex - 1)
                }
            }
        },
        closeRight(rightActive: string, activeTab: string) {
            const rStore = routerStore()
            let rightIndex = this.tabsList.findIndex((item) => {
                return item.path === rightActive
            })
            const len = this.tabsList.length
            for (let index = len - 1; index >= 0; index--) {
                if (index > rightIndex) {
                    const item = this.tabsList[index]
                    // console.log('item=', item)
                    if (item.path !== activeTab) {
                        rStore.delKeepAliveRouters(item.name)
                    }
                }
            }
            let activeIndex = this.tabsList.findIndex((item) => {
                return item.path === activeTab
            })
            rightIndex = rightIndex
            activeIndex = activeIndex
            console.log('rightIndex=', rightIndex)
            console.log('activeIndex=', activeIndex)
            if (rightIndex + 1 < len) {
                if (activeIndex > rightIndex) {
                    if (activeIndex - 1 > rightIndex) {
                        this.tabsList.splice(
                            activeIndex + 1,
                            len - activeIndex - 1
                        )
                        this.tabsList.splice(
                            rightIndex + 1,
                            activeIndex - rightIndex - 1
                        )
                    } else {
                        this.tabsList.splice(
                            activeIndex + 1,
                            len - activeIndex - 1
                        )
                    }
                } else {
                    this.tabsList.splice(rightIndex + 1, len - rightIndex - 1)
                }
            }
        },
        closeOther(rightActive: string) {
            const rStore = routerStore()
            let rightIndex = this.tabsList.findIndex((item) => {
                if (item.path != rightActive) {
                    // console.log('item=', item)
                    rStore.delKeepAliveRouters(item.name)
                }
                return item.path === rightActive
            })
            const len = this.tabsList.length
            console.log('rightIndex=', rightIndex)
            console.log('len=', len)
            if (rightIndex + 1 == len) {
                this.tabsList.splice(1, rightIndex - 1)
            } else if (rightIndex == 1) {
                this.tabsList.splice(2, len - 2)
            } else {
                this.tabsList.splice(rightIndex + 1, len - rightIndex - 1)
                this.tabsList.splice(1, rightIndex - 1)
            }
        }
    },
    getters: {
        getTabs(state) {
            return state.tabsList
        }
    }
})
